import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useLoadingStore = defineStore('loading', () => {
  // 路由加载状态
  const isRouteLoading = ref(false)
  
  // 加载计数器，支持多个并发加载
  const loadingCount = ref(0)
  
  // 显示路由加载动画
  const showRouteLoading = () => {
    loadingCount.value++
    isRouteLoading.value = true
  }
  
  // 隐藏路由加载动画
  const hideRouteLoading = () => {
    loadingCount.value = Math.max(0, loadingCount.value - 1)
    if (loadingCount.value === 0) {
      isRouteLoading.value = false
    }
  }
  
  // 强制隐藏所有加载状态
  const forceHideLoading = () => {
    loadingCount.value = 0
    isRouteLoading.value = false
  }
  
  // 获取当前加载状态
  const getLoadingState = () => ({
    isRouteLoading: isRouteLoading.value,
    loadingCount: loadingCount.value
  })
  
  return {
    isRouteLoading,
    loadingCount,
    showRouteLoading,
    hideRouteLoading,
    forceHideLoading,
    getLoadingState
  }
})
